//
//  YJYPictureBrowsersPushAnimation.m
//  BannerViewDemo
//
//  Created by BaizYi on 2018/11/5.
//  Copyright © 2018 BaizYi. All rights reserved.
//

#import "YJYPictureBrowsersPushAnimation.h"
#import "YJYPictureBrowsersTransitionParamer.h"

@implementation YJYPictureBrowsersPushAnimation

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.25;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIView *containerView = [transitionContext containerView];
    
    //ToVC
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    toViewController.view.hidden = YES;
    [containerView addSubview:toViewController.view];
    
    //fromVC
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    //图片背景白色的空白view
    UIImageView *imgBgWhiteView = [[UIImageView alloc]initWithFrame:[self.transitionParamer.firstVCImageFrames[self.transitionParamer.transitionImgIndex] CGRectValue]];
//    imgBgWhiteView.backgroundColor = [UIColor whiteColor];
    imgBgWhiteView.image = self.transitionParamer.transitionImage;
    [containerView addSubview:imgBgWhiteView];
    
    //有渐变的黑色背景
    UIView *bgView = [[UIView alloc]initWithFrame:containerView.bounds];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0;
    [containerView addSubview:bgView];
    
    //过渡的图片
    UIImageView *transitionImg = [[UIImageView alloc]initWithFrame:[self.transitionParamer.firstVCImageFrames[self.transitionParamer.transitionImgIndex] CGRectValue]];
    transitionImg.image = self.transitionParamer.transitionImage;
    [transitionContext.containerView addSubview:transitionImg];
    
    
    //动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        transitionImg.frame = self.transitionParamer.secendVCImgFrame;
        bgView.alpha = 1;
    } completion:^(BOOL finished) {
        toViewController.view.hidden = NO;
        [imgBgWhiteView removeFromSuperview];
        [bgView removeFromSuperview];
        [transitionImg removeFromSuperview];
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
    
    
    
}

@end
